<template>
  <div class="api-fix-test">
    <h2>API响应码修复测试</h2>
    
    <div class="test-section">
      <h3>测试MCI接口响应码处理</h3>
      <van-button @click="testMCIResponse" :loading="testing">
        {{ testing ? '测试中...' : '测试MCI接口响应' }}
      </van-button>
      <div v-if="testResult" class="result">
        <p><strong>状态:</strong> {{ testResult.status }}</p>
        <p><strong>消息:</strong> {{ testResult.message }}</p>
        <pre v-if="testResult.data">{{ JSON.stringify(testResult.data, null, 2) }}</pre>
      </div>
    </div>

    <div class="test-section">
      <h3>模拟后端响应测试</h3>
      <van-button @click="testMockResponse" :loading="testingMock">
        {{ testingMock ? '测试中...' : '测试模拟响应' }}
      </van-button>
      <div v-if="mockResult" class="result">
        <p><strong>状态:</strong> {{ mockResult.status }}</p>
        <p><strong>消息:</strong> {{ mockResult.message }}</p>
        <pre v-if="mockResult.data">{{ JSON.stringify(mockResult.data, null, 2) }}</pre>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Toast } from 'vant'
import { api } from '@/api'

const testing = ref(false)
const testingMock = ref(false)
const testResult = ref<any>(null)
const mockResult = ref<any>(null)

// 测试MCI接口响应码处理
const testMCIResponse = async () => {
  testing.value = true
  testResult.value = null
  
  try {
    // 测试获取就诊人列表（读接口）
    const response = await api.getPatients()
    testResult.value = {
      status: '成功',
      message: 'MCI读接口测试成功',
      data: response
    }
    Toast('MCI读接口测试成功')
  } catch (error) {
    console.error('MCI接口测试失败:', error)
    testResult.value = {
      status: '失败',
      message: error instanceof Error ? error.message : '未知错误',
      data: null
    }
    Toast('MCI接口测试失败')
  } finally {
    testing.value = false
  }
}

// 测试模拟响应
const testMockResponse = async () => {
  testingMock.value = true
  mockResult.value = null
  
  try {
    // 模拟一个会返回code: 400但message为"修改成功"的请求
    const mockData = {
      relationship: '本人',
      name: '测试用户',
      idCardType: '身份证',
      idCardNum: '123456789012345678',
      medicalCard: '20250101001',
      phone: '13800138000',
      address: '测试地址',
      isDefault: false
    }
    
    // 这里会调用写接口，应该返回code: 400但message为"修改成功"
    const response = await api.addPatient(mockData)
    mockResult.value = {
      status: '成功',
      message: '模拟响应测试成功',
      data: response
    }
    Toast('模拟响应测试成功')
  } catch (error) {
    console.error('模拟响应测试失败:', error)
    mockResult.value = {
      status: '失败',
      message: error instanceof Error ? error.message : '未知错误',
      data: null
    }
    Toast('模拟响应测试失败')
  } finally {
    testingMock.value = false
  }
}
</script>

<style scoped>
.api-fix-test {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.test-section {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}

.test-section h3 {
  margin-top: 0;
  color: #1989fa;
}

.result {
  margin-top: 15px;
  padding: 15px;
  background: #f7f8fa;
  border-radius: 4px;
}

.result pre {
  background: #fff;
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
  font-size: 12px;
}
</style>

